package firstping.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import firstping.data.QnAPersistenceManager;
import firstping.model.Customer;

@SuppressWarnings("serial")
public class ActiveServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		String md5 = req.getParameter("active");
		
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		
		Query query = psm.newQuery(Customer.class);
    	query.setFilter("md5=='"+md5+"'");
    	@SuppressWarnings("unchecked")
		List<Customer> cus = (List<Customer>) query.execute();
		
    	if(cus.size() > 0)
    	{
    		cus.get(0).setStatus(1);
    		
    		psm=JDOHelper.getPersistenceManager(cus.get(0));
			psm.currentTransaction().begin();
			psm.makePersistent(cus.get(0));
			psm.currentTransaction().commit();
    	} else {
    		req.setAttribute("error", "error");
    	}
    
    	req.setAttribute("title", "Active account");
		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
		req.setAttribute("description", "Active account.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
    	
    	try {
			req.getRequestDispatcher("/active.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
